﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using System.IO;
using System.Collections;

namespace TotalSystems.Core.Controls
{
    public partial class GridViewExportPopup : ChildWindow
    {
        public RadGridView RadGridView_Page;
        public RadDataPager RadDataPager_Page;
        public bool HasColumnHeaders = true;
        public bool HasColumnFooters = true;
        public bool HasGroupFooters = true;

        public GridViewExportPopup(RadGridView RGV,RadDataPager RDP)
        {
            InitializeComponent();
            RadGridView_Page = RGV;
            RadDataPager_Page = RDP;
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Export(string Type)
        {
            string extension = "";
            ExportFormat format = ExportFormat.Html;

            switch (Type)
            {
                case "Excel":
                    extension = "xls";
                    format = ExportFormat.Html;
                    break;
                case "xml":
                    extension = "xml";
                    format = ExportFormat.ExcelML;
                    break;
                case "Word":
                    extension = "doc";
                    format = ExportFormat.Html;
                    break;
                case "Csv":
                    extension = "csv";
                    format = ExportFormat.Csv;
                    break;
            }


            SaveFileDialog dialog = new SaveFileDialog()
            {
                DefaultExt = extension,
                Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, Type),
                FilterIndex = 1
            };

            if (dialog.ShowDialog() == true)
            {
                int originalPageSize = RadDataPager_Page.PageSize;
                int originalPageIndex = RadDataPager_Page.PageIndex;

                using (Stream stream = dialog.OpenFile())
                {
                    RadDataPager_Page.PageSize = 0;
                    GridViewExportOptions exportOptions = new GridViewExportOptions();
                    exportOptions.Format = format;
                    exportOptions.ShowColumnFooters = HasColumnFooters;
                    exportOptions.ShowColumnHeaders = HasColumnHeaders;
                    exportOptions.ShowGroupFooters = HasGroupFooters;
                    exportOptions.Items = (IEnumerable)RadDataPager_Page.PagedSource;

                    RadGridView_Page.Export(stream, exportOptions);
                }

                RadDataPager_Page.PageSize = originalPageSize;
                RadDataPager_Page.PageIndex = originalPageIndex;


            }
        }

        public void SetGrid_ForExportToFile(bool hasColumnHeaders, bool hasColumnFooters, bool hasGroupFooters)
        {
            HasColumnHeaders = hasColumnHeaders;
            HasColumnFooters = hasColumnFooters;
            HasGroupFooters = hasGroupFooters;
        }

        private void radBTN_Export_Click(object sender, RoutedEventArgs e)
        {
            switch ((sender as RadButton).Name)
            {
                case "radBTN_Excel":
                    Export("Excel");
                    break;
                case "radBBTN_Word":
                    Export("Word");
                    break;
                case "radBTN_XML":
                    Export("xml");
                    break;
                case "radBTN_CSV":
                    Export("Csv");
                    break;
            }
        }
        
    }
}

